import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  videoList: {},
  currentVideo: {},
  currentVideoQuestion: {},
  videoQuestionList: {},
  searchVideoCondition: {
    pageSize: 10,
    title: ""
  }
};

const getters = {
  videoList: state => state.videoList,
  currentVideo: state => state.currentVideo,
  searchVideoCondition: state => state.searchVideoCondition
};

const actions = {
  getVideoPageS({ commit }, param) {
    return Api.getVideoPage(param).then(ret => {
      commit(types.UPDATE_VIDEO_LIST, ret.data.data);
      return ret;
    });
  },
  getVideoS({ commit }, id) {
    return Api.getVideo({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_VIDEO, ret.data.data);
      return ret;
    });
  },
  addOrUpdateVideoS({ commit }, payload) {
    return Api.addOrUpdateVideo({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteVideoS({ commit }, param) {
    return Api.deleteVideo(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.UPDATE_VIDEO_LIST](state, payload) {
    state.videoList = payload;
  },
  [types.INIT_CURRENT_VIDEO](state, payload) {
    state.currentVideo = {
      title: "",
      video: ""
    };
  },
  [types.UPDATE_CURRENT_VIDEO](state, payload) {
    state.currentVideo = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
